/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import DAOClass.Members;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Phong
 */
public class singin extends HttpServlet {

    Members objMember = new Members();

    /*  memberInsert method - return a Interger
     * if InsertSucessfuly - return 0
     * the UserName exist - return 1
     * the Email exist - return 2
     * Insert error - return -1
     */
    public int memberInsert(HttpServletRequest request, HttpServletResponse response) {
        boolean flag;
        int returnInterger = 0;


        String user = request.getParameter("user");
        int IDG = 2;
        String pass = request.getParameter("pass");
        String name = request.getParameter("fullname");
        int sex = Integer.parseInt(request.getParameter("sex"));
        String birth = request.getParameter("birthday");
        String tel = request.getParameter("tel");
        String add = request.getParameter("address");
        String mail = request.getParameter("mail");
        int status = 1;

        if (objMember.checkSameMember(user)) {
            if (objMember.checkSameEmail(mail)) {
                if (objMember.Singin(user, IDG, pass, name, sex, birth, tel, add, mail, status)) {
                    return returnInterger;
                } else {
                    returnInterger = -1;
                }
            } else {
                returnInterger = 2;
            }
        } else {
            returnInterger = 1;
        }
        return returnInterger;
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet singin</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet singin at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Process Singin</title>");
            if (memberInsert(request, response) == 0) {
                out.println("<h2>Insert Successful</h2>");
            } else if (memberInsert(request, response) == -1) {
                out.println("<h2>Insert error</h2>" + memberInsert(request, response));
            } else if (memberInsert(request, response) == 1) {
                response.sendRedirect("register.jsp?u=exist");
            } else
                response.sendRedirect("register.jsp?m=exist");
            out.println("</head>");
            out.println("<body>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
